<template>
  <el-dialog :title="showTitle" :visible="dialogVisible" @close="handleClose">
    <el-form
      ref="roleDialogForm"
      label-width="80px"
      :model="roleForm"
      :rules="rules"
    >
      <el-form-item
        prop="regionName"
        label="区域名称"
      >
        <el-input v-model="roleForm.regionName" placeholder="请输入" maxlength="15" show-word-limit />
      </el-form-item>
      <el-form-item label="备注说明" prop="remark">
        <el-input v-model="roleForm.remark" type="textarea" row="5" placeholder="请输入备注(不超过40字)" maxlength="40" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button v-loading="loading" type="primary" @click="handleSumit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addPointApi, updataPointApi } from '@/api/point'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roleForm: {
        regionName: '',
        remark: ''
      },
      rules: {
        regionName: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        remark: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  computed: {
    showTitle() {
      return this.roleForm.id ? '编辑区域' : '新增区域'
    }
  },
  methods: {
    handleClose() {
      this.$emit('update:dialogVisible', false)
      this.$refs.roleDialogForm.resetFields()
      this.roleForm = {}
    },
    async handleSumit() {
      try {
        await this.$refs.roleDialogForm.validate()
        this.roleForm.id ? await updataPointApi(this.roleForm) : await addPointApi(this.roleForm)
        this.handleClose()
        this.$message.success(`${this.roleForm.id ? '修改' : '增添'}区域成功`)
        this.$parent.getPage()
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style>

</style>
